<?php
/**
 * Created by PhpStorm.
 * User: u
 * Date: 2019/3/31
 * Time: 17:03
 */

namespace common\sources\read\buydepositrecord;

use Yii;
use common\models\craftsman\BuyDepositRecord;
use yii\data\Pagination;

class BuyDepositRecordAccess implements IBuyDepositRecordAccess {
	public function UnusualBankList() {
		$sql  = "SELECT id,buy_id,balance FROM buy_deposit_record WHERE create_time IN (SELECT MAX(create_time) FROM `buy_deposit_record` GROUP BY buy_id)";
		$list = Yii::$app->db->createCommand($sql)->queryAll();
		return $list;
	}

	//买手押金记录
	public function getBuyDepositRecordCount($where) {
		return BuyDepositRecord::find()
			->where($where)
			->count();
	}

	//买手押金列表
	public function getBuyDepositRecordList($data,$where) {
		return BuyDepositRecord::find()
			->select("id,task_id,task_commod_id,task_order_id,task_order_num,expend,income,balance,freeze_change,freeze_balance,remark,create_time,img_url,action_type,operatorId,auditor_user,end_user")
			->where($where)
			->orderBy("create_time desc ")
			->offset(($data['page_num'] - 1) * $data['limit'])
			->limit($data['limit'])
			->asArray()
			->all();
	}

	/**
	 * getPageList
	 * @param $where
	 * @param $orderBy
	 * @param $limit
	 * @author 中崋 (1291713293@qq.com)
	 * @date   2019/5/10
	 * @return mixed
	 */
	public function getPageList($where, $orderBy = "create_time desc", $limit = 10) {
		$query = BuyDepositRecord::find()
			->alias('a')
			->select("u.phone, a.id, a.task_id, a.task_commod_id, a.task_order_id, a.task_order_num, a.expend, a.income, a.balance, a.remark, a.create_time, a.img_url, a.action_type, a.operatorId")
			->leftJoin('buyuser u', 'u.id = a.buy_id')
			->where($where)
			->orderBy($orderBy);

		$page            = new Pagination(['totalCount' => $query->count(), 'pageSize' => $limit, 'pageParam' => 'page_num']);
		$result['list']  = $query->offset($page->offset)->limit($page->limit)->asArray()->all();
		$result['count'] = $page->totalCount;
		return $result;
	}
}